<?
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Advertisement extends CI_Controller 
{
	function __construct()
	{
		parent::__construct();
		
		//load helper
		$this->load->helper('login_helper');
		
			//load model
$this->load->model('Msub_cate');
		$this->load->model('Mads');
		


    //load libraries

    $this->load->library('pagination');
	load_lang("admin");
    $this->load->library('form_validation');
	// session
	 session_start();
		 if ( !isset($_SESSION['row_number']))

			$_SESSION['row_number']= 12;
	}

	function index($page='')
	{
	
		$limit = $_SESSION['row_number'];

		$config['base_url'] = site_url().'/admin/advertisement/index/';

		$config['first_link'] = 'First';

		$config['last_link'] = 'Last';

		$config['next_link'] = '&gt;&gt;';

		$config['prev_link'] = '&lt;&lt;';

		$config['cur_tag_open'] = '<a href="#" class="current">';

		$config['cur_tag_close'] = '</a>';

		$config['uri_segment'] = 4;

		$config['num_links'] = 2;

		$config['total_rows'] = $this->Mads->count_all();

		$config['per_page'] = $limit;
		

		$this->pagination->initialize($config);

    if (empty($page))	$page = 0; //default value

    $data['result'] = $this->Mads->list_data($page,$limit);// $this->Muser->user_review($page, $limit);

    $data['title'] = 'Danh sách quảng cáo';

    $data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());

    $data['main'] = 'ads_review';

    $this->load->view('admin/template_main', $data);
	
	}

	function create()
	{
		 if ($this->input->post('create'))

    {
		
    

    $this->form_validation->set_rules('title_en', 'English Name', 'required');

      $this->form_validation->set_rules('title_vi', 'Vietnamese Name', 'required');

      $config['upload_path'] = './upload/ads/';
	  if (!is_dir(  $config['upload_path']))
	  {
	  	mkdir($config['upload_path']);
	  }
	  	$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = '500';
		$config['max_width'] = '500';
		$config['max_height'] = '500';
	
		$this->load->library('upload',$config);
 

      if ($this->form_validation->run() && $this->sub_create() )

        redirect('admin/advertisement/', 'refresh');

      else
		{
			
        	$data['missing'] = $this->form_validation->_error_array;

		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/advertisement/', 'refresh');

		}



	
		$data['main'] = 'ads_create';
			
//$data['cate'] = $this->Msub_cate->get_sub_cate();
//$data['unit'] = $this->Mproduct->get_unit();
    $data['title'] = $this->lang->line('title_product_create');

		$this->load->view('admin/template_main', $data);
		
		
	}
	//proccess add produ
	function sub_create()
	{
		
		if(isset($_POST["create"]))
		{
			if($_FILES['img']['name'] != NULL )
				{
					if(!$this->upload->do_upload("img"))
					{	
						$data['missing']['img']= $this->upload->display_errors();
						$data['result'] = $this->Mads->info_from_id($id);
	
					//	$data['cate'] = $this->Msub_cate->get_sub_cate();
						$data['main'] = 'ads_update';
					
						$data['title'] = $this->lang->line('title_product_update');
				
						$this->load->view('admin/template_main', $data);
						
						return false;		
					//	$this->load->view('admin/template_main', $data);
				}
				else
				{
					//$upload
				}
				
			}
			if(isset($_POST['active']))
			{
				$active = 1;
			}
			else
				$active = 0;
		
	
			$info1 = array("vi" => htmlentities($this->input->post("detailed_content_vi"),ENT_QUOTES,"UTF-8"),
			
			"en" => htmlentities($this->input->post("detailed_content_en"),ENT_QUOTES,"UTF-8")
			);
			$name = array("vi" => htmlentities($this->input->post("title_vi"),ENT_QUOTES,"UTF-8"),
			"kd"=> unicodeConvert($this->input->post("title_vi")),
			"en" => htmlentities($this->input->post("title_en"),ENT_QUOTES,"UTF-8"),);
		
			//	print_r($info);
			$url= $this->input->post("url");
			$date_expire = $this->input->post("date");
		
		$data = array(
			"name" =>serialize($name),
			"info" => serialize($info1),
			"type"=> $this->input->post("cate"),
			"date_begin"=> date('d-m-Y H:i:s'),
			"date_expire" => $date_expire ,
			"url" => $url,
			"priority"=> $this->input->post("priority"),
			"image" =>  $_FILES['img']['name'] ,
			"actived" =>$active,
			"position" => $this->input->post("position"),
			
			);
			
	//
		//	print_r($data);
		if($this->Mads->insert($data))
			return true;
		else
			return false;
		}
		else
			return false;
		
	
	
	}
		
	function update()
	{
	$id= $this->uri->segment(4);
		 if ($this->input->post('update'))

    {
	

      //set rules


      $this->form_validation->set_rules('title_en', 'Title English', 'required');

      $this->form_validation->set_rules('title_vi', 'Title Vietnamese', 'required');

	 
    //  $this->form_validation->set_rules('detailed_content_en', 'English detail', 'required');

//      $this->form_validation->set_rules('detailed_content_vi', 'Vietnamese detail', 'required');

 	
      $config['upload_path'] = './upload/ads/';
	  if (!is_dir(  $config['upload_path']))
	  {
	  	mkdir($config['upload_path']);
	  }
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = '500';
		$config['max_width'] = '500';
		$config['max_height'] = '500';
	
		$this->load->library('upload',$config);

   
		if ($this->form_validation->run() == false )
		{	$data['missing'] = $this->form_validation->_error_array;

}
      else
		{
			
			if($_FILES['img']['name'] != NULL )
				{
					if(!$this->upload->do_upload("img"))
					{	
						$data['missing']['img']= $this->upload->display_errors();
						$data['result'] = $this->Mads->info_from_id($id);
	
					//	$data['cate'] = $this->Msub_cate->get_sub_cate();
						$data['main'] = 'ads_update';
					
						$data['title'] = $this->lang->line('title_product_update');
				
						$this->load->view('admin/template_main', $data);
						
						return false;		
					//	$this->load->view('admin/template_main', $data);
					}
					else
					{
						if( $_FILES['img']['error'] != 4 )
						{
							$file = $this->Mads->info_from_id($id);
						
							$path = "./upload/ads/".$file[0]['image'];
						//	echo $path;
							unlink($path);
							
						}
					}
				}
		
			
		
			if($this->prod_update_pro())
        	        redirect('admin/advertisement/', 'refresh');
			
		}
    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/advertisement/', 'refresh');

		}
	//$data['unit'] = $this->Mads->get_unit();
		$data['main'] = 'ads_update';
	$data['result'] = $this->Mads->info_from_id($id);
	
$data['cate'] = $this->Msub_cate->get_sub_cate();
    $data['title'] = $this->lang->line('title_product_update');

		$this->load->view('admin/template_main', $data);
	}
	
	// proccess updating product
	function prod_update_pro()
	{
		$id= $this->uri->segment(4);
		if(isset($_POST["update"]))
		{
		/*	if($this->upload_file() ==  true && $_FILES['img']['error'] != 4 )
			{
				$file = $this->Mproduct->info_from_id($id,'product');
				
				$path = "./upload/product/".$file[0]['image'];
				unlink($path);
				
			}*/
			if(isset($_POST['active']))
			{
				$active = 1;
			}
			else
				$active = 0;
		
	
			$info1 = array("vi" => htmlentities($this->input->post("detailed_content_vi"),ENT_QUOTES,"UTF-8"),
			
			"en" => htmlentities($this->input->post("detailed_content_en"),ENT_QUOTES,"UTF-8")
			);
			$name = array("vi" => htmlentities($this->input->post("title_vi"),ENT_QUOTES,"UTF-8"),
			"kd"=> unicodeConvert($this->input->post("title_vi")),
			"en" => htmlentities($this->input->post("title_en"),ENT_QUOTES,"UTF-8"),);
		
			//	print_r($info);
			$url= $this->input->post("url");
			$date_expire = $this->input->post("date");
		
	
			
			if($_FILES['img']['error'] == 0)
			{	
			$hinh = $this->upload->data();
			$path = "./upload/ads/".$hinh['file_name'];	
				$data = array(
			"name" =>serialize($name),
			"info" => serialize($info1),
			"type"=> $this->input->post("cate"),
			"date_begin"=> date('d-m-Y H:i:s'),
			"date_expire" => $date_expire ,
			"url" => $url,
			"priority"=> $this->input->post("priority"),
			"image" =>  $_FILES['img']['name'] ,
			"actived" =>$active,
			"position" => $this->input->post("position"),);
	
			}
			else
				$data = array(
			"name" =>serialize($name),
			"info" => serialize($info1),
			"type"=> $this->input->post("cate"),
			"date_begin"=> date('d-m-Y H:i:s'),
			"date_expire" => $date_expire ,
			"url" => $url,
			"priority"=> $this->input->post("priority"),
			"actived" =>$active,
			"position" => $this->input->post("position"),);
		
			//print_r($data);
				
			if($this->Mads->update_product($data,$id))
				return true;
			else
				return false;
		}
		else
			return false;
		
	
	
	}
	
	
	
	function request()

	{

		//determine 'action'
	 

		$require = array('show', 'delete');

		foreach ($_POST as $key=>$value)

		{

			if (in_array($key, $require))

			{

				$act = $key;
				
			}

		}



		//process 'action'

		if (! empty($act))

		{

			switch ($act)

			{

				case 'show': //show

				{
				
					$_SESSION['row_number'] = $this->input->post('row_number');
					
				} break;



				case 'delete': //delete

				{

					$this->Mads->delete();

				} break;

			}//end switch



			redirect('admin/product/', 'refresh');

		}//end if

	}
	
}









?>
